<!DOCTYPE HTML>
<html lang="zh-CN">


<head>
    <meta charset="utf-8">
    <meta name="keywords" content="STM32CubeMX  TFT-LCD">
    <meta name="description" content="嵌入式 STM32 物联网领域的教程及精彩内容分享">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
    <meta name="renderer" content="webkit|ie-stand|ie-comp">
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <!-- Global site tag (gtag.js) - Google Analytics -->


    <title>STM32CubeMX | 35-使用硬件FSMC驱动TFT-LCD屏幕（MCU屏，NT35510控制器） | Mculover666</title>
    <link rel="icon" type="image/png" href="/favicon.png">

    <link rel="stylesheet" type="text/css" href="/libs/awesome/css/all.css">
    <link rel="stylesheet" type="text/css" href="/libs/materialize/materialize.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/aos/aos.css">
    <link rel="stylesheet" type="text/css" href="/libs/animate/animate.min.css">
    <link rel="stylesheet" type="text/css" href="/libs/lightGallery/css/lightgallery.min.css">
    <link rel="stylesheet" type="text/css" href="/css/matery.css">
    <link rel="stylesheet" type="text/css" href="/css/my.css">

    <script src="/libs/jquery/jquery.min.js"></script>

<meta name="generator" content="Hexo 5.4.0"></head>




<body>
    <header class="navbar-fixed">
    <nav id="headNav" class="bg-color nav-transparent">
        <div id="navContainer" class="nav-wrapper container">
            <div class="brand-logo">
                <a href="/" class="waves-effect waves-light">
                    
                    <img src="/medias/logo.png" class="logo-img" alt="LOGO">
                    
                    <span class="logo-span">Mculover666</span>
                </a>
            </div>
            

<a href="#" data-target="mobile-nav" class="sidenav-trigger button-collapse"><i class="fas fa-bars"></i></a>
<ul class="right nav-menu">
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/" class="waves-effect waves-light">
      
      <i class="fas fa-home" style="zoom: 0.6;"></i>
      
      <span>首页</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="" class="waves-effect waves-light">

      
      <i class="fas fa-list" style="zoom: 0.6;"></i>
      
      <span>系列教程</span>
      <i class="fas fa-chevron-down" aria-hidden="true" style="zoom: 0.6;"></i>
    </a>
    <ul class="sub-nav menus_item_child ">
      
      <li>
        <a href="/posts/578764034/">
          
          <span>STM32CubeMX</span>
        </a>
      </li>
      
    </ul>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a target="_blank" rel="noopener" href="https://blog.csdn.net/Mculover666" class="waves-effect waves-light">
      
      <i class="fas fa-bookmark" style="zoom: 0.6;"></i>
      
      <span>CSDN</span>
    </a>
    
  </li>
  
  <li class="hide-on-med-and-down nav-item">
    
    <a href="/download" class="waves-effect waves-light">
      
      <i class="fas fa-download" style="zoom: 0.6;"></i>
      
      <span>资源下载</span>
    </a>
    
  </li>
  
  <li>
    <a href="#searchModal" class="modal-trigger waves-effect waves-light">
      <i id="searchIcon" class="fas fa-search" title="搜索" style="zoom: 0.85;"></i>
    </a>
  </li>
</ul>


<div id="mobile-nav" class="side-nav sidenav">

    <div class="mobile-head bg-color">
        
        <img src="/medias/logo.png" class="logo-img circle responsive-img">
        
        <div class="logo-name">Mculover666</div>
        <div class="logo-desc">
            
            嵌入式 STM32 物联网领域的教程及精彩内容分享
            
        </div>
    </div>

    

    <ul class="menu-list mobile-menu-list">
        
        <li class="m-nav-item">
	  
		<a href="/" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-home"></i>
			
			首页
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="javascript:;">
			
				<i class="fa-fw fas fa-list"></i>
			
			系列教程
			<span class="m-icon"><i class="fas fa-chevron-right"></i></span>
		</a>
            <ul  style="background:  ;" >
              
                <li>

                  <a href="/posts/578764034/ " style="margin-left:75px">
				  
		          <span>STM32CubeMX</span>
                  </a>
                </li>
              
            </ul>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a target="_blank" rel="noopener" href="https://blog.csdn.net/Mculover666" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-bookmark"></i>
			
			CSDN
		</a>
          
        </li>
        
        <li class="m-nav-item">
	  
		<a href="/download" class="waves-effect waves-light">
			
			    <i class="fa-fw fas fa-download"></i>
			
			资源下载
		</a>
          
        </li>
        
        
        <li><div class="divider"></div></li>
        <li>
            <a href="https://github.com/blinkfox/hexo-theme-matery" class="waves-effect waves-light" target="_blank">
                <i class="fab fa-github-square fa-fw"></i>Fork Me
            </a>
        </li>
        
    </ul>
</div>


        </div>

        
            <style>
    .nav-transparent .github-corner {
        display: none !important;
    }

    .github-corner {
        position: absolute;
        z-index: 10;
        top: 0;
        right: 0;
        border: 0;
        transform: scale(1.1);
    }

    .github-corner svg {
        color: #0f9d58;
        fill: #fff;
        height: 64px;
        width: 64px;
    }

    .github-corner:hover .octo-arm {
        animation: a 0.56s ease-in-out;
    }

    .github-corner .octo-arm {
        animation: none;
    }

    @keyframes a {
        0%,
        to {
            transform: rotate(0);
        }
        20%,
        60% {
            transform: rotate(-25deg);
        }
        40%,
        80% {
            transform: rotate(10deg);
        }
    }
</style>

<a href="https://github.com/blinkfox/hexo-theme-matery" class="github-corner tooltipped hide-on-med-and-down" target="_blank"
   data-tooltip="Fork Me" data-position="left" data-delay="50">
    <svg viewBox="0 0 250 250" aria-hidden="true">
        <path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path>
        <path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2"
              fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path>
        <path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z"
              fill="currentColor" class="octo-body"></path>
    </svg>
</a>
        
    </nav>

</header>

    



<div class="bg-cover pd-header post-cover" style="background-image: url('/medias/featureimages/23.jpg')">
    <div class="container" style="right: 0px;left: 0px;">
        <div class="row">
            <div class="col s12 m12 l12">
                <div class="brand">
                    <h1 class="description center-align post-title">STM32CubeMX | 35-使用硬件FSMC驱动TFT-LCD屏幕（MCU屏，NT35510控制器）</h1>
                </div>
            </div>
        </div>
    </div>
</div>




<main class="post-container content">

    
    <link rel="stylesheet" href="/libs/tocbot/tocbot.css">
<style>
    #articleContent h1::before,
    #articleContent h2::before,
    #articleContent h3::before,
    #articleContent h4::before,
    #articleContent h5::before,
    #articleContent h6::before {
        display: block;
        content: " ";
        height: 100px;
        margin-top: -100px;
        visibility: hidden;
    }

    #articleContent :focus {
        outline: none;
    }

    .toc-fixed {
        position: fixed;
        top: 64px;
    }

    .toc-widget {
        width: 345px;
        padding-left: 20px;
    }

    .toc-widget .toc-title {
        padding: 35px 0 15px 17px;
        font-size: 1.5rem;
        font-weight: bold;
        line-height: 1.5rem;
    }

    .toc-widget ol {
        padding: 0;
        list-style: none;
    }

    #toc-content {
        padding-bottom: 30px;
        overflow: auto;
    }

    #toc-content ol {
        padding-left: 10px;
    }

    #toc-content ol li {
        padding-left: 10px;
    }

    #toc-content .toc-link:hover {
        color: #42b983;
        font-weight: 700;
        text-decoration: underline;
    }

    #toc-content .toc-link::before {
        background-color: transparent;
        max-height: 25px;

        position: absolute;
        right: 23.5vw;
        display: block;
    }

    #toc-content .is-active-link {
        color: #42b983;
    }

    #floating-toc-btn {
        position: fixed;
        right: 15px;
        bottom: 76px;
        padding-top: 15px;
        margin-bottom: 0;
        z-index: 998;
    }

    #floating-toc-btn .btn-floating {
        width: 48px;
        height: 48px;
    }

    #floating-toc-btn .btn-floating i {
        line-height: 48px;
        font-size: 1.4rem;
    }
</style>
<div class="row">
    <div id="main-content" class="col s12 m12 l9">
        <!-- 文章内容详情 -->
<div id="artDetail">
    <div class="card">
        <div class="card-content article-info">
            <div class="row tag-cate">
                <div class="col s7">
                    
                    <div class="article-tag">
                        
                            <a href="/tags/STM32CubeMX-TFT-LCD/">
                                <span class="chip bg-color">STM32CubeMX TFT-LCD</span>
                            </a>
                        
                    </div>
                    
                </div>
                <div class="col s5 right-align">
                    
                    <div class="post-cate">
                        <i class="fas fa-bookmark fa-fw icon-category"></i>
                        
                            <a href="/categories/STM32CubeMX/" class="post-category">
                                STM32CubeMX
                            </a>
                        
                    </div>
                    
                </div>
            </div>

            <div class="post-info">
                
                <div class="post-date info-break-policy">
                    <i class="far fa-calendar-minus fa-fw"></i>发布日期:&nbsp;&nbsp;
                    2020-09-23
                </div>
                

                

                

                

                
                    <div id="busuanzi_container_page_pv" class="info-break-policy">
                        <i class="far fa-eye fa-fw"></i>阅读次数:&nbsp;&nbsp;
                        <span id="busuanzi_value_page_pv"></span>
                    </div>
				
            </div>
        </div>
        <hr class="clearfix">

        

        

        <div class="card-content article-card-content">
            <div id="articleContent">
                <p>本篇详细的记录了如何使用STM32CubeMX配置 STM32f407ZGT6 的硬件FSMC外设驱动TFT-LCD屏幕。</p>
<span id="more"></span>

<h1 id="1-准备工作"><a href="#1-准备工作" class="headerlink" title="1. 准备工作"></a>1. 准备工作</h1><h2 id="硬件准备"><a href="#硬件准备" class="headerlink" title="硬件准备"></a>硬件准备</h2><ul>
<li><p>开发板<br>首先需要准备一个开发板，这里我准备的是STM32F407ZGT6的开发板。</p>
</li>
<li><p>TFT-LCD<br>开发板底板接正点原子4.3寸TFT-LCD。<br><img src="https://img-blog.csdnimg.cn/20200902144001942.png#pic_center"></p>
<h1 id="2-STM32-FSMC外设概述"><a href="#2-STM32-FSMC外设概述" class="headerlink" title="2. STM32 FSMC外设概述"></a>2. STM32 FSMC外设概述</h1><h2 id="2-1-什么是FSMC"><a href="#2-1-什么是FSMC" class="headerlink" title="2.1. 什么是FSMC"></a>2.1. 什么是FSMC</h2><p>FSMC全称 Flexible static memory controller，灵活的<strong>静态内存控制器</strong>，顾名思义，其主要作用是：<font color="red"><strong>负责向外部扩展的存储类设备提供控制信号</strong></font>。</p>
</li>
</ul>
<p>FSMC内存控制器支持的存储设备有：</p>
<ul>
<li>Nor Flash、SRAM、PSRAM</li>
<li>Nand Flash</li>
<li>类SRAM设备</li>
</ul>
<h2 id="2-2-FSMC外设的功能框图"><a href="#2-2-FSMC外设的功能框图" class="headerlink" title="2.2. FSMC外设的功能框图"></a>2.2. FSMC外设的功能框图</h2><p><img src="https://img-blog.csdnimg.cn/20200902095825816.png#pic_center" alt="在这里插入图片描述"></p>
<h2 id="2-3-外部设备的地址映射（重点）"><a href="#2-3-外部设备的地址映射（重点）" class="headerlink" title="2.3. 外部设备的地址映射（重点）"></a>2.3. 外部设备的地址映射（重点）</h2><p>从FSMC的角度来看，外部的存储设备被分为几个固定大小的Bank，<font color="red"><strong>每个bank 256 MB</strong></font>。</p>
<p>整个FSMC外设映射地址的划分如图：<br><img src="https://img-blog.csdnimg.cn/20200902100004516.png#pic_center"></p>
<h3 id="2-3-1-Bank1"><a href="#2-3-1-Bank1" class="headerlink" title="2.3.1. Bank1"></a>2.3.1. Bank1</h3><p>Bank1的地址空间为：<code>0x6000 0000 - 0x6FFF FFFF</code>，支持外接Nor Flash、PSRAM、SRAM等设备，还可以外接DM9000等类存储设备。</p>
<p>整个Bank1的地址空间被划分为四个子bank，<strong>每个子bank的大小为64MB，刚好对应FSMC外设的地址总线（FSMC_A[0:25]）有26条（2^26=64MB）。</strong></p>
<p>FSMC还有两条内部总线ADDR[27:26]，用这两路控制片选信号，如下表：</p>
<p><img src="https://img-blog.csdnimg.cn/20200825164300784.png#pic_center"></p>
<h4 id="BANK1控制时序模型"><a href="#BANK1控制时序模型" class="headerlink" title="BANK1控制时序模型"></a>BANK1控制时序模型</h4><p>接下来讲述BANK1控制外部存储器的时序模式，BANK1又称为Nor Flash/SRAM/PSRAM控制器，后续暂且叫它SRAM控制器。</p>
<p>SRAM控制器支持两种控制模式：</p>
<ul>
<li>同步模式</li>
<li>异步模式</li>
</ul>
<p>对于异步模式，FSMC主要设置三个时序参数：</p>
<ul>
<li>地址建立时间：ADDSET</li>
<li>数据建立时间：DATASET</li>
<li>地址保持时间：ADDHLD</li>
</ul>
<p>根据SRAM、PSRAM、Nor Flash的综合特点，FMC定义了四种不同的异步时序模型，如下表：<br><img src="https://img-blog.csdnimg.cn/20200827160826912.png#pic_center"><br><font color="red"><strong>本文中控制TFT-LCD使用的就是异步ModeA时序模型</strong>。</font></p>
<h4 id="异步ModeA时序模型"><a href="#异步ModeA时序模型" class="headerlink" title="异步ModeA时序模型"></a>异步ModeA时序模型</h4><p>模式A时序模型的优势在于：<strong>支持独立的读写时序控制</strong>。这一点对于控制TFT-LCD来说，非常符合。因为TFT-LCD在读的时候，一般比较慢，而在写入的时候一般比较快。</p>
<p>模式A的读操作时序如图：<br><img src="https://img-blog.csdnimg.cn/20200827161640276.png#pic_center"><br>模式A的写操作时序如图：<br><img src="https://img-blog.csdnimg.cn/20200827161220796.png#pic_center"><br>图中ADDSET和DATASET两个时序的值，后续配置的时候会详细讲述。</p>
<h3 id="2-3-2-Bank2、3-4"><a href="#2-3-2-Bank2、3-4" class="headerlink" title="2.3.2. Bank2、3/4"></a>2.3.2. Bank2、3/4</h3><p>只能外接Nand Flash设备和PC Card设备：<br><img src="https://img-blog.csdnimg.cn/2020090210025130.png#pic_center"></p>
<h1 id="3-使用STM32CubeMX生成工程"><a href="#3-使用STM32CubeMX生成工程" class="headerlink" title="3. 使用STM32CubeMX生成工程"></a>3. 使用STM32CubeMX生成工程</h1><h2 id="选择芯片型号"><a href="#选择芯片型号" class="headerlink" title="选择芯片型号"></a>选择芯片型号</h2><p>打开STM32CubeMX，打开MCU选择器：<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL21jdWxvdmVyNjY2LmNuL2ltYWdlLzIwMTkwODA2L2dCUDZnbG1VU0g4MC5wbmc?x-oss-process=image/format,png"></p>
<p>搜索并选中芯片<code>STM32F407ZGT6</code>：<br><img src="https://img-blog.csdnimg.cn/20200902100922112.png#pic_center"></p>
<h2 id="配置时钟源"><a href="#配置时钟源" class="headerlink" title="配置时钟源"></a>配置时钟源</h2><ul>
<li>如果选择使用外部高速时钟（HSE），则需要在System Core中配置RCC；</li>
<li>如果使用默认内部时钟（HSI），这一步可以略过；</li>
</ul>
<p>这里我都使用外部时钟：<br><img src="https://img-blog.csdnimg.cn/20200902101014202.png#pic_center"></p>
<h2 id="调试选项配置"><a href="#调试选项配置" class="headerlink" title="调试选项配置"></a><font color="red">调试选项配置</font></h2><p>默认没有配置下载引脚，烧录之后下载器将无法再检测到，这里我使用ST-Link，所以配置为SW选项：<br><img src="https://img-blog.csdnimg.cn/20200902101308732.png#pic_center"></p>
<h2 id="配置串口"><a href="#配置串口" class="headerlink" title="配置串口"></a>配置串口</h2><p>开发板板载了一个CH340换串口，连接到USART1，但是引脚不是默认引脚，需要手动修改。</p>
<p>接下来开始配置<code>USART1</code>：<img src="https://img-blog.csdnimg.cn/20200902101420886.png#pic_center"></p>
<h2 id="配置FSMC外设"><a href="#配置FSMC外设" class="headerlink" title="配置FSMC外设"></a>配置FSMC外设</h2><blockquote>
<p><font color="blue"><strong>本文所使用的开发板中，将TFT-LCD当做SRAM来操作，连接在FSMC的BANK1的第4个区域。</strong></font><br>知识点：为什么TFT-LCD可以当做SRAM来控制？<br>因为TFT-LCD和SRAM相比，同样需要D0-D15数据线，WR、RD、CS控制线，唯一不同的就是TFT-LCD需要一条RS信号线（用于控制传输的是命令还是数据），而SRAM则需要一堆地址线，所以可以巧妙的使用任意一条地址线来当做RS信号。</p>
</blockquote>
<h3 id="FSMC配置"><a href="#FSMC配置" class="headerlink" title="FSMC配置"></a>FSMC配置</h3><p>开发板上 TFT-LCD 的原理图如下：<br><img src="https://img-blog.csdnimg.cn/20200902144453108.png#pic_center"></p>
<p>通过原理图可以看出：</p>
<ul>
<li>LCD D0-D15：使用了16bit：FSMC D0 - FSMC D15；</li>
<li>LCD_RS：<strong>使用FSMC A6来控制向LCD写入数据还是命令</strong>（0-命令，1-数据）；</li>
<li>LCD_BL：背光控制，对应PB5；</li>
<li>LCD_CS：LCD片选信号，FMC_NE4，表示使用Bank1的Bank4子区域</li>
<li>LCD_WR ：LCD写使能，FSMC_NWE；</li>
<li>LCD_RD：LCD读使能，FSMC_NOE；</li>
<li>RESET：LCD复位信号，直接与单片机复位信号接在一起；</li>
</ul>
<p>根据这些信息，在STM32CubeMX中先配置SRAM4的基本设置：<br><img src="https://img-blog.csdnimg.cn/20200902153545792.png#pic_center"></p>
<blockquote>
<p>此处如果选择LCD接口类型和SRAM类型的区别在于：<br>LCD接口类型只会配置用到的那一个地址引脚，而SRAM类型则会配置所有的地址引脚。</p>
</blockquote>
<h3 id="SRAM基本参数配置"><a href="#SRAM基本参数配置" class="headerlink" title="SRAM基本参数配置"></a>SRAM基本参数配置</h3><p>首先设置基本的参数，允许读与写使用不同的模式：<br><img src="https://img-blog.csdnimg.cn/20200902145359604.png#pic_center"></p>
<h3 id="SRAM时序参数配置"><a href="#SRAM时序参数配置" class="headerlink" title="SRAM时序参数配置"></a>SRAM时序参数配置</h3><p>本文中使用的LCD控制器为NT35510控制器，找到其数据手册，查看：<br><img src="https://img-blog.csdnimg.cn/20200903133437456.png#pic_center"><br>其中主要的时序参数配置方法如下。</p>
<h4 id="读时序配置"><a href="#读时序配置" class="headerlink" title="读时序配置"></a>读时序配置</h4><p>① HCLK</p>
<p>时序参数都是以HCLK的周期为单位的，在本文中HCLK=168Mhz，所以一个周期为5.95ns。</p>
<p>② 地址建立时间：Address setup time（ADDSET）</p>
<p>该时序的最大值的15个HCLK，从图中可以看出，<strong>NT35110控制器要求读的时候最小为10ns，,所以设为2即可，2x5.95=11.9ns</strong>。</p>
<p>③ 数据持续时间：Data setup time(DATASET)</p>
<p>读时序比较慢，该时序的最大值为255个HCLK，从图中可以看出，<strong>NT35510控制器要求的数据建立时间最小为15ns，但因为读时序比较慢，所以设为4，4x5.95=23.8ns。</strong></p>
<h4 id="写时序配置"><a href="#写时序配置" class="headerlink" title="写时序配置"></a>写时序配置</h4><p>① HCLK</p>
<p>时序参数都是以HCLK的周期为单位的，在本文中HCLK=168Mhz，所以一个周期为5.95ns。</p>
<p>② 地址建立时间：Address setup time（ADDSET）</p>
<p>该时序的最大值为15个HCLK，<strong>NT35110控制器要求写的时候最小为0，,所以设为0即可</strong>。</p>
<p>③ 数据持续时间：Data setup time(DATASET)</p>
<p>写时序比较快，该时序的最大值为255个HCLK，图中可以看出，<strong>NT35510控制器要求的数据建立时间最小为15ns，但因为读时序比较慢，所以设为3，3x5.95=17.85ns。</strong></p>
<p>综合上述计算，配置情况如下：<br><img src="https://img-blog.csdnimg.cn/20200903134229114.png#pic_center"></p>
<h2 id="配置背光引脚"><a href="#配置背光引脚" class="headerlink" title="配置背光引脚"></a>配置背光引脚</h2><p><img src="https://img-blog.csdnimg.cn/20200902150606678.png#pic_center"></p>
<h2 id="配置时钟树"><a href="#配置时钟树" class="headerlink" title="配置时钟树"></a>配置时钟树</h2><p>STM32F407ZGT6的最高主频到168M，使<code>HCLK = 168Mhz</code>即可：<br><img src="https://img-blog.csdnimg.cn/20200902112508876.png#pic_center" alt="在这里插入图片描述"></p>
<h2 id="生成工程设置"><a href="#生成工程设置" class="headerlink" title="生成工程设置"></a>生成工程设置</h2><p><img src="https://img-blog.csdnimg.cn/20200902104659374.png#pic_center"></p>
<h2 id="代码生成设置"><a href="#代码生成设置" class="headerlink" title="代码生成设置"></a>代码生成设置</h2><p>最后设置生成独立的初始化文件：<br><img src="https://imgconvert.csdnimg.cn/aHR0cDovL21jdWxvdmVyNjY2LmNuL2ltYWdlLzIwMTkwODA2L1Q2V3ZTSzZEZnB0cy5wbmc?x-oss-process=image/format,png"></p>
<h2 id="生成代码"><a href="#生成代码" class="headerlink" title="生成代码"></a>生成代码</h2><p>点击<code>GENERATE CODE</code>即可生成MDK-V5工程：<br><img src="https://img-blog.csdnimg.cn/20200902104733463.png#pic_center"></p>
<h1 id="4-编写TFT-LCD驱动（测试是否可以正常读写ID）"><a href="#4-编写TFT-LCD驱动（测试是否可以正常读写ID）" class="headerlink" title="4. 编写TFT-LCD驱动（测试是否可以正常读写ID）"></a>4. 编写TFT-LCD驱动（测试是否可以正常读写ID）</h1><p><font color="red"><strong>特别提醒：STM32CubeMX生成的工程默认开启了-O3优化，编写的驱动太菜了，会出问题，所以遇到玄学Bug请改为-O0优化！</strong></font></p>
<h2 id="封装底层发送-读取函数"><a href="#封装底层发送-读取函数" class="headerlink" title="封装底层发送/读取函数"></a>封装底层发送/读取函数</h2><p>LCD的底层无非就是两个API：<strong>发送命令、发送数据</strong>，（有的还需要从屏幕读取数据），接下来封装出这两（三）个底层API。</p>
<p>之前查看原理图的时候，表示命令或者数据的LCD_RS控制引脚接在FMC_A6上，也就是说地址数据的第6位，所以在头文件<code>lcd-fsmc.h</code>中先定义：</p>
<pre><code class="c">/* 通过地址线控制RS引脚 */
#define LCD_CMD_ADDR            0x6c00007E
#define LCD_DAT_ADDR            0x6c000080
</code></pre>
<p>接着开始封装两个（三个）底层操作函数：</p>
<p>① 发送命令函数：</p>
<pre><code class="c">/**
 * @brief    向LCD写入命令
 * @param    cmd 待写入命令
 * @retval   none
*/
static void lcd_write_cmd(__IO uint16_t cmd)
&#123;
    *(uint16_t *)(LCD_CMD_ADDR) = cmd;
&#125;
</code></pre>
<p>② 发送数据函数：</p>
<pre><code class="c">/**
 * @brief    向LCD写入数据
 * @param    data 待写入数据
 * @retval   none
*/
static void lcd_write_data(__IO uint16_t data)
&#123;
    *(uint16_t *)(LCD_DAT_ADDR) = data;
&#125;
</code></pre>
<p>③ 读取数据函数：</p>
<pre><code class="c">/**
 * @brief    从LCD读取数据
 * @param    none
 * @retval   读取到的数据
*/
static uint16_t lcd_read_data(void)
&#123;
    __IO uint16_t data;
    
    data = *(uint16_t *)(LCD_DAT_ADDR);
    
    return data;
&#125;
</code></pre>
<p>基于这三个底层API，还可以封装出读写LCD内部寄存器的函数：</p>
<pre><code class="c">/**
 * @brief    写LCD中的寄存器
 * @param    reg  寄存器序号
 * @param    data 要写入寄存器的值
 * @retval   none
*/
static void lcd_write_reg(__IO uint16_t reg, __IO uint16_t data)
&#123;
    lcd_write_cmd(reg);
    lcd_write_data(data);
&#125;
</code></pre>
<h2 id="LCD控制参数结构体"><a href="#LCD控制参数结构体" class="headerlink" title="LCD控制参数结构体"></a>LCD控制参数结构体</h2><p>为了方便驱动不同的IC，保存不同的控制参数，在<code>lcd_fmc.h</code>中封装如下数据类型：</p>
<pre><code class="c">/**
 * @brief    保存LCD屏幕参数
 * @param    lcd_width     LCD屏幕宽度
 * @param    lcd_height    LCD屏幕高度
 * @param    lcd_id        LCD 驱动IC ID
 * @param    lcd_direction LCD横屏显示还是竖屏显示，0-竖屏，1-横屏
 * @param    wram_cmd      开始写gram指令
 * @param    set_x_cmd     设置x坐标指令
 * @param    set_y_cmd     设置y坐标指令
*/
typedef struct lcd_params_st &#123;
    uint16_t lcd_width;
    uint16_t lcd_height;
    uint16_t lcd_id;
    uint8_t  lcd_direction;
    uint16_t wram_cmd;
    uint16_t set_x_cmd;
    uint16_t set_y_cmd;
&#125; lcd_params_t;
</code></pre>
<p>然后在头文件中声明外部变量定义，方便其他程序访问：</p>
<pre><code class="c">extern lcd_params_t lcd_params;
</code></pre>
<p>在<code>lcd_fsmc.c</code>中定义此变量为全局变量：</p>
<pre><code class="c">lcd_params_t lcd_params;
</code></pre>
<h2 id="LCD驱动打印日志的处理"><a href="#LCD驱动打印日志的处理" class="headerlink" title="LCD驱动打印日志的处理"></a>LCD驱动打印日志的处理</h2><p>为了方便程序开发，难免要打印一些日志，但是如果printf没有被重定向，则会导致LCD驱动卡死。为了避免这个问题，我们使用宏开关的方式来控制是否打印。</p>
<p>在<code>lcd_fsmc.h</code>中定义此宏开关：</p>
<pre><code class="c">/* 使能此驱动是否打印调试日志（需要printf支持） */
#define LCD_LOG_ENABLE          1
</code></pre>
<p>接着可以定义一个日志打印函数：</p>
<pre><code class="c">#if LCD_LOG_ENABLE
#include &lt;stdio.h&gt;
#define LCD_LOG printf
#else
#define LCD_LOG(format,...)
#endif
</code></pre>
<p>之后所以需要打印的地方使用<code>LCD_LOG</code>代替printf即可。</p>
<h2 id="编写LCD控制器ID读取函数"><a href="#编写LCD控制器ID读取函数" class="headerlink" title="编写LCD控制器ID读取函数"></a>编写LCD控制器ID读取函数</h2><p>通过主动读取此控制器ID，可以自动检测出是哪种类型的控制器，然后执行不同的驱动代码：</p>
<pre><code class="c">static int lcd_read_id(void)
&#123;
    /* 尝试执行ILI9341控制器ID的读取流程 */
    lcd_write_cmd(0XD3);                   
    lcd_params.lcd_id = lcd_read_data();
    lcd_params.lcd_id = lcd_read_data();
    lcd_params.lcd_id = lcd_read_data();                   
    lcd_params.lcd_id &lt;&lt;= 8;
    lcd_params.lcd_id |= lcd_read_data();
    /* 如果正常读到，则返回成功 */
    if (lcd_params.lcd_id == 0x9341) &#123;
        return 0;
    &#125;
    
    /* 尝试执行NT35310控制器ID的读取流程 */
    lcd_write_cmd(0XD4);                   
    lcd_params.lcd_id = lcd_read_data();
    lcd_params.lcd_id = lcd_read_data();
    lcd_params.lcd_id = lcd_read_data();
    lcd_params.lcd_id &lt;&lt;= 8;     
    lcd_params.lcd_id |= lcd_read_data();
    /* 如果正常读到，则返回成功 */
    if (lcd_params.lcd_id == 0x5310) &#123;
        return 0;
    &#125;
    
    /* 尝试执行NT35510控制器ID的读取流程 */
    lcd_write_cmd(0XDA00);    
    lcd_params.lcd_id = lcd_read_data();
    lcd_write_cmd(0XDB00);    
    lcd_params.lcd_id = lcd_read_data();
    lcd_params.lcd_id &lt;&lt;= 8;     
    lcd_write_cmd(0XDC00);    
    lcd_params.lcd_id |= lcd_read_data();
    /* 如果正常读到，则返回成功 */
    if (lcd_params.lcd_id == 0x8000) &#123;
        lcd_params.lcd_id = 0x5510;
        return 0;
    &#125;
   
    /* 驱动IC不支持 */
    lcd_params.lcd_id = 0;
    return -1;
&#125;
</code></pre>
<h2 id="编写LCD初始化函数"><a href="#编写LCD初始化函数" class="headerlink" title="编写LCD初始化函数"></a>编写LCD初始化函数</h2><p>LCD初始化需要发送大量的命令和数据，本文限于篇幅，只给出读LCD 控制IC的ID的部分，用来测试LCD是否能正常读写足矣。</p>
<pre><code class="c">void lcd_init(void)
&#123;     
    /* 初始化FMC接口 */
    //MX_FSMC_Init();
    
    /* 开启背光 */
    HAL_GPIO_WritePin(LCD_BL_GPIO_Port, LCD_BL_Pin, GPIO_PIN_SET);
    
     HAL_Delay(50); 
    
     /* 读取LCD控制器IC */
    if (lcd_read_id() == -1) &#123;
        LCD_LOG(&quot;Not Support LCD IC!\r\n&quot;);
        return;
    &#125; else &#123;
        LCD_LOG(&quot;LCD IC ID is:%#x\r\n&quot;, lcd_params.lcd_id);  
    &#125;
    
    return;
&#125;

</code></pre>
<p>在<code>lcd_fsmc.h</code>中声明该函数：</p>
<pre><code class="c">void lcd_init(void);
</code></pre>
<h2 id="测试是否可以正常操作LCD"><a href="#测试是否可以正常操作LCD" class="headerlink" title="测试是否可以正常操作LCD"></a>测试是否可以正常操作LCD</h2><p>在<code>main.c</code>中包含进来头文件：</p>
<pre><code class="c">/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include &lt;stdio.h&gt;
#include &quot;lcd_fsmc.h&quot;
/* USER CODE END Includes */
</code></pre>
<p>然后在man函数中调用：</p>
<pre><code class="c">/* USER CODE BEGIN 2 */
printf(&quot;4.3&#39; TFT-LCD Test By Mculover666\r\n&quot;);
lcd_init();
/* USER CODE END 2 */
</code></pre>
<p>编译，下载，在串口助手中查看结果：<br><img src="https://img-blog.csdnimg.cn/20200902203758221.png#pic_center"></p>
<h1 id="5-编写TFT-LCD驱动（初始化、刷屏测试）"><a href="#5-编写TFT-LCD驱动（初始化、刷屏测试）" class="headerlink" title="5. 编写TFT-LCD驱动（初始化、刷屏测试）"></a>5. 编写TFT-LCD驱动（初始化、刷屏测试）</h1><p>可以正常读取ID之后，接下来的工作是：</p>
<ul>
<li>发送一堆一堆的命令，初始化屏幕；</li>
<li>设置坐标</li>
<li>清屏</li>
<li>刷屏测试</li>
</ul>
<p>① LCD开显示、关显示、LCD设置扫描方向、 LCD设置显示方向、LCD设置光标位置这些函数代码不多，需要的话请查看源码。</p>
<p>② 清屏函数：</p>
<pre><code class="c">
static void lcd_write_ram_start(void)
&#123;
    lcd_write_cmd(lcd_params.wram_cmd);
&#125;

static void lcd_write_ram(uint16_t rgb_color)
&#123;
    lcd_write_data(rgb_color);
&#125;

void lcd_clear(uint16_t color)
&#123;
    uint32_t index = 0;      
    uint32_t totalpoint = lcd_params.lcd_width;
    
    /* 计算得到总点数 */
    totalpoint *= lcd_params.lcd_height;
    
    /* 设置光标位置 */
    lcd_set_cursor(0x00,0x0000);
    
    /* 开始写入GRAM */
    lcd_write_ram_start();
    
    /* 写入数据到GRAM */
    for (index = 0; index &lt; totalpoint; index++) &#123;
        lcd_write_ram(color);
    &#125;
&#125;
</code></pre>
<p>③ 初始化函数：代码过长，请查看源码。</p>
<p>这三类函数实现完之后，就可以编写一个如下的刷屏测试函数：</p>
<pre><code class="c">void lcd_auto_clear(uint16_t period_ms)
&#123;
    lcd_clear(BLACK);
    HAL_Delay(period_ms);
    lcd_clear(BLUE);
    HAL_Delay(period_ms);
    lcd_clear(GREEN);
    HAL_Delay(period_ms);
    lcd_clear(GBLUE);
    HAL_Delay(period_ms);
    lcd_clear(CYAN);
    HAL_Delay(period_ms);
    lcd_clear(GRAY);
    HAL_Delay(period_ms);
    lcd_clear(BROWN);
    HAL_Delay(period_ms);
    lcd_clear(RED);
    HAL_Delay(period_ms);
    lcd_clear(BRED);
    HAL_Delay(period_ms);
    lcd_clear(BRRED);
    HAL_Delay(period_ms);
    lcd_clear(YELLOW);
    HAL_Delay(period_ms);
    lcd_clear(WHITE);
    HAL_Delay(period_ms);
&#125;
</code></pre>
<p>在main函数中调用此函数，分别给予不同的刷新频率，测试刷屏速度和效果。</p>
<h1 id="6-实现打点、画线、填充函数（重点）"><a href="#6-实现打点、画线、填充函数（重点）" class="headerlink" title="6. 实现打点、画线、填充函数（重点）"></a>6. 实现打点、画线、填充函数（重点）</h1><h2 id="打点函数"><a href="#打点函数" class="headerlink" title="打点函数"></a>打点函数</h2><pre><code class="c">/**
 * @brief    LCD打点函数
 * @param    x_pos x方向坐标
 * @param    y_pos y方向坐标
 * @retval   none
*/
void lcd_draw_point(uint16_t x_pos, uint16_t y_pos, uint16_t color)
&#123;
    if (x_pos &gt; lcd_params.lcd_width || y_pos &gt; lcd_params.lcd_height) &#123;
        return;
    &#125;
    
    lcd_set_cursor(x_pos, y_pos);
    lcd_write_ram_start();
    lcd_write_ram(color);
&#125;
</code></pre>
<h2 id="设置窗口函数"><a href="#设置窗口函数" class="headerlink" title="设置窗口函数"></a>设置窗口函数</h2><pre><code class="c">/**
 * @brief    LCD设置窗口
 * @param    x_pos_start x方向起始坐标
 * @param    y_pos_start y方向起始坐标
 * @param    width       窗口宽度
 * @param    height      窗口高度
 * @retval   none
 * @note     此函数执行完，坐标在窗口左上角
*/
void lcd_set_window(uint16_t x_pos_start, uint16_t y_pos_start, uint16_t width, uint16_t height)
&#123;
    uint16_t x_pos_end, y_pos_end;

    x_pos_end = x_pos_start + width - 1;
    y_pos_end = y_pos_start + height - 1;
    
    if (x_pos_end &lt; x_pos_start || x_pos_end &gt; lcd_params.lcd_width) &#123;
        return;
    &#125;
    
    if (y_pos_end &lt; y_pos_start || y_pos_end &gt; lcd_params.lcd_height) &#123;
        return;
    &#125;
    
    if(lcd_params.lcd_id == 0x9341 || lcd_params.lcd_id == 0x5310) &#123;
        lcd_write_cmd(lcd_params.set_x_cmd); 
        lcd_write_data(x_pos_start &gt;&gt; 8); 
        lcd_write_data(x_pos_start &amp; 0xFF);     
        lcd_write_data(x_pos_end &gt;&gt; 8); 
        lcd_write_data(x_pos_end &amp; 0xFF);  
        lcd_write_cmd(lcd_params.set_y_cmd); 
        lcd_write_data(y_pos_start &gt;&gt; 8); 
        lcd_write_data(y_pos_start &amp; 0xFF); 
        lcd_write_data(y_pos_end &gt;&gt; 8); 
        lcd_write_data(y_pos_end &amp; 0xFF); 
    &#125; else if (lcd_params.lcd_id == 0x5510) &#123;
        lcd_write_cmd(lcd_params.set_x_cmd); 
        lcd_write_data(x_pos_start &gt;&gt; 8); 
        lcd_write_cmd(lcd_params.set_x_cmd + 1);
        lcd_write_data(x_pos_start &amp; 0xFF);      
        lcd_write_cmd(lcd_params.set_x_cmd + 2);
        lcd_write_data(x_pos_end &gt;&gt; 8);   
        lcd_write_cmd(lcd_params.set_x_cmd + 3);
        lcd_write_data(x_pos_end &amp; 0xFF);   
        lcd_write_cmd(lcd_params.set_y_cmd);
        lcd_write_data(y_pos_start &gt;&gt; 8);   
        lcd_write_cmd(lcd_params.set_y_cmd + 1);
        lcd_write_data(y_pos_start&amp;0xFF);  
        lcd_write_cmd(lcd_params.set_y_cmd + 2);
        lcd_write_data(y_pos_end &gt;&gt; 8);   
        lcd_write_cmd(lcd_params.set_y_cmd + 3);
        lcd_write_data(y_pos_end &amp; 0xFF);  
    &#125;
&#125;
</code></pre>
<h2 id="画线函数"><a href="#画线函数" class="headerlink" title="画线函数"></a>画线函数</h2><pre><code class="c">/**
 * @brief   LCD画线函数
 * @param   x1 x方向起始坐标
 * @param   x2 x方向终止坐标
 * @param   y1 y方向起始坐标
 * @param   y2 y方向终止坐标
 * @return  none
 */
void lcd_draw_line(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
&#123;
    uint16_t    i = 0;
    int16_t        delta_x = 0, delta_y = 0;
    int8_t        incx = 0, incy = 0;
    uint16_t    distance = 0;
    uint16_t    t = 0;
    uint16_t    x = 0, y = 0;
    uint16_t     x_temp = 0, y_temp = 0;
    
    if(y1 == y2)
    &#123;
     /* 快速画水平线 */
        lcd_set_window(x1, y1, x2, y2);
        lcd_write_ram_start();
        for(i = 0; i &lt; x2 - x1; i++)
        &#123;
            lcd_write_ram(color);
        &#125;

        return;
    &#125;
    else
    &#123;
        /* 画斜线（Bresenham算法） */
        /* 计算两点之间在x和y方向的间距，得到画笔在x和y方向的步进值 */
        delta_x = x2 - x1;
        delta_y = y2 - y1;
        if(delta_x &gt; 0)
        &#123;
            //斜线(从左到右)
            incx = 1;
        &#125;
        else if(delta_x == 0)
        &#123;
            //垂直斜线(竖线)
            incx = 0;
        &#125;
        else
        &#123;
            //斜线(从右到左)
            incx = -1;
            delta_x = -delta_x;
        &#125;
        if(delta_y &gt; 0)
        &#123;
            //斜线(从左到右)
            incy = 1;
        &#125;
        else if(delta_y == 0)
        &#123;
            //水平斜线(水平线)
            incy = 0;
        &#125;
        else
        &#123;
            //斜线(从右到左)
            incy = -1;
            delta_y = -delta_y;
        &#125;            
        
        /* 计算画笔打点距离(取两个间距中的最大值) */
        if(delta_x &gt; delta_y)
        &#123;
            distance = delta_x;
        &#125;
        else
        &#123;
            distance = delta_y;
        &#125;
        
        /* 开始打点 */
        x = x1;
        y = y1;
        //第一个点无效，所以t的次数加一
        for(t = 0; t &lt;= distance + 1;t++)
        &#123;
            lcd_draw_point(x, y, color);
        
            /* 判断离实际值最近的像素点 */
            x_temp += delta_x;    
            if(x_temp &gt; distance)
            &#123;
                //x方向越界，减去距离值，为下一次检测做准备
                x_temp -= distance;        
                //在x方向递增打点
                x += incx;
                    
            &#125;
            y_temp += delta_y;
            if(y_temp &gt; distance)
            &#123;
                //y方向越界，减去距离值，为下一次检测做准备
                y_temp -= distance;
                //在y方向递增打点
                y += incy;
            &#125;
        &#125;
    &#125;
&#125;
</code></pre>
<h2 id="画矩形函数"><a href="#画矩形函数" class="headerlink" title="画矩形函数"></a>画矩形函数</h2><pre><code class="c">/**
 * @breif    LCD画矩形
 * @param   x1 x方向起始坐标
 * @param   x2 x方向终止坐标
 * @param   y1 y方向起始坐标
 * @param   y2 y方向终止坐标
 * @param    color 颜色
 * @retval    none
 */
void lcd_draw_rect(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
&#123;
    lcd_draw_line(x1,y1,x2,y1,color);
    lcd_draw_line(x1,y1,x1,y2,color);
    lcd_draw_line(x1,y2,x2,y2,color);
    lcd_draw_line(x2,y1,x2,y2,color);
&#125;
</code></pre>
<h2 id="画圆函数"><a href="#画圆函数" class="headerlink" title="画圆函数"></a>画圆函数</h2><pre><code class="c">/**
 * @breif    LCD画圆函数
 * @param   x x方向坐标
 * @param    y 方向坐标
 * @param   r 半径
 * @param    color 颜色
 * @retval    none
 */
void lcd_draw_circle(uint16_t x, uint16_t y, uint16_t r, uint16_t color)
&#123;
    /* Bresenham画圆算法 */
    int16_t a = 0, b = r;
    int16_t d = 3 - (r &lt;&lt; 1);        //算法决策参数
        
    /* 如果圆在屏幕可见区域外，直接退出 */
    if (x - r &lt; 0 || x + r &gt; lcd_params.lcd_width || y - r &lt; 0 || y + r &gt; lcd_params.lcd_height) &#123;
        return;
    &#125;
        
    /* 开始画圆 */
    while(a &lt;= b)
    &#123;
        lcd_draw_point(x - b, y - a, color);
        lcd_draw_point(x + b, y - a, color);
        lcd_draw_point(x - a, y + b, color);
        lcd_draw_point(x - b, y - a, color);
        lcd_draw_point(x - a, y - b, color);
        lcd_draw_point(x + b, y + a, color);
        lcd_draw_point(x + a, y - b, color);
        lcd_draw_point(x + a, y + b, color);
        lcd_draw_point(x - b, y + a, color);
        a++;

        if(d &lt; 0)
            d += 4 * a + 6;
        else
        &#123;
            d += 10 + 4 * (a - b);
            b--;
        &#125;

        lcd_draw_point(x + a, y + b, color);
    &#125;
&#125;
</code></pre>
<h2 id="矩形填充函数"><a href="#矩形填充函数" class="headerlink" title="矩形填充函数"></a>矩形填充函数</h2><pre><code class="c">/**
 * @breif    LCD填充一个矩形区域
 * @param   x1 x方向起始坐标
 * @param   x2 x方向终止坐标
 * @param   y1 y方向起始坐标
 * @param   y2 y方向终止坐标
 * @param    color 颜色
 * @retval    none
 */
void lcd_fill(uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, uint16_t color)
&#123;
    uint16_t i, j;
    uint32_t xlen = 0;
    
    xlen = x2 - x1 + 1;
    for (i = y1; i &lt;= y2; i++) &#123;
        lcd_set_cursor(x1, i);
        lcd_write_ram_start();
        for (j = 0; j &lt; xlen; j++) &#123;
            lcd_write_ram(color);
        &#125;
    &#125;
&#125;
</code></pre>
<h2 id="测试显示"><a href="#测试显示" class="headerlink" title="测试显示"></a>测试显示</h2><p>在main函数中编写如下的代码，测试显示：</p>
<pre><code class="c"> //画线测试
lcd_clear(BLACK);
lcd_draw_line(0, lcd_params.lcd_height/2, lcd_params.lcd_width, lcd_params.lcd_height/2, BLUE);
lcd_draw_line(lcd_params.lcd_width/2, 0, lcd_params.lcd_width/2, lcd_params.lcd_height, YELLOW);
lcd_draw_line(0, 0, lcd_params.lcd_width, lcd_params.lcd_height, GREEN);
lcd_draw_line(lcd_params.lcd_width, 0, 0, lcd_params.lcd_height, RED);

//画矩形测试
lcd_draw_rect(lcd_params.lcd_width/4, lcd_params.lcd_height/4, lcd_params.lcd_width/4*3, lcd_params.lcd_height/4*3, CYAN);

//画圆测试
lcd_draw_circle(lcd_params.lcd_width/2, lcd_params.lcd_height/2, lcd_params.lcd_width/4, BRED);
</code></pre>
<p>编译，下载，在竖屏显示时，效果图如下：<br><img src="https://img-blog.csdnimg.cn/20200903095321869.png#pic_center"><br>改为横屏显示，效果如下：<br><img src="https://img-blog.csdnimg.cn/20200903095448712.png#pic_center"></p>
<h1 id="7-字符显示"><a href="#7-字符显示" class="headerlink" title="7. 字符显示"></a>7. 字符显示</h1><p>这部分自己做也可以，但是用GUI可以实现更多好玩的东西，就测试到这里啦~</p>
<p><font color="red"><strong>更多精彩文章及资源，请关注我的微信公众号：『mculover666』</strong></font>。</p>
<p><img src="https://img-blog.csdnimg.cn/20200827112436112.png#pic_center"></p>

                
            </div>
            <hr/>

            

    <div class="reprint" id="reprint-statement">
        
            <div class="reprint__author">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-user">
                        文章作者:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="/about" rel="external nofollow noreferrer">Mculover666</a>
                </span>
            </div>
            <div class="reprint__type">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-link">
                        文章链接:
                    </i>
                </span>
                <span class="reprint-info">
                    <a href="http://www.mculover666.cn/posts/1132663314/">http://www.mculover666.cn/posts/1132663314/</a>
                </span>
            </div>
            <div class="reprint__notice">
                <span class="reprint-meta" style="font-weight: bold;">
                    <i class="fas fa-copyright">
                        版权声明:
                    </i>
                </span>
                <span class="reprint-info">
                    本博客所有文章除特別声明外，均采用
                    <a href="https://creativecommons.org/licenses/by/4.0/deed.zh" rel="external nofollow noreferrer" target="_blank">CC BY 4.0</a>
                    许可协议。转载请注明来源
                    <a href="/about" target="_blank">Mculover666</a>
                    !
                </span>
            </div>
        
    </div>

    <script async defer>
      document.addEventListener("copy", function (e) {
        let toastHTML = '<span>复制成功，请遵循本文的转载规则</span><button class="btn-flat toast-action" onclick="navToReprintStatement()" style="font-size: smaller">查看</a>';
        M.toast({html: toastHTML})
      });

      function navToReprintStatement() {
        $("html, body").animate({scrollTop: $("#reprint-statement").offset().top - 80}, 800);
      }
    </script>



            <div class="tag_share" style="display: block;">
                <div class="post-meta__tag-list" style="display: inline-block;">
                    
                        <div class="article-tag">
                            
                                <a href="/tags/STM32CubeMX-TFT-LCD/">
                                    <span class="chip bg-color">STM32CubeMX TFT-LCD</span>
                                </a>
                            
                        </div>
                    
                </div>
                <div class="post_share" style="zoom: 80%; width: fit-content; display: inline-block; float: right; margin: -0.15rem 0;">
                    <link rel="stylesheet" type="text/css" href="/libs/share/css/share.min.css">
<div id="article-share">

    
    <div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>
    <script src="/libs/share/js/social-share.min.js"></script>
    

    

</div>

                </div>
            </div>
            
                <style>
    #reward {
        margin: 40px 0;
        text-align: center;
    }

    #reward .reward-link {
        font-size: 1.4rem;
        line-height: 38px;
    }

    #reward .btn-floating:hover {
        box-shadow: 0 6px 12px rgba(0, 0, 0, 0.2), 0 5px 15px rgba(0, 0, 0, 0.2);
    }

    #rewardModal {
        width: 320px;
        height: 350px;
    }

    #rewardModal .reward-title {
        margin: 15px auto;
        padding-bottom: 5px;
    }

    #rewardModal .modal-content {
        padding: 10px;
    }

    #rewardModal .close {
        position: absolute;
        right: 15px;
        top: 15px;
        color: rgba(0, 0, 0, 0.5);
        font-size: 1.3rem;
        line-height: 20px;
        cursor: pointer;
    }

    #rewardModal .close:hover {
        color: #ef5350;
        transform: scale(1.3);
        -moz-transform:scale(1.3);
        -webkit-transform:scale(1.3);
        -o-transform:scale(1.3);
    }

    #rewardModal .reward-tabs {
        margin: 0 auto;
        width: 210px;
    }

    .reward-tabs .tabs {
        height: 38px;
        margin: 10px auto;
        padding-left: 0;
    }

    .reward-content ul {
        padding-left: 0 !important;
    }

    .reward-tabs .tabs .tab {
        height: 38px;
        line-height: 38px;
    }

    .reward-tabs .tab a {
        color: #fff;
        background-color: #ccc;
    }

    .reward-tabs .tab a:hover {
        background-color: #ccc;
        color: #fff;
    }

    .reward-tabs .wechat-tab .active {
        color: #fff !important;
        background-color: #22AB38 !important;
    }

    .reward-tabs .alipay-tab .active {
        color: #fff !important;
        background-color: #019FE8 !important;
    }

    .reward-tabs .reward-img {
        width: 210px;
        height: 210px;
    }
</style>

<div id="reward">
    <a href="#rewardModal" class="reward-link modal-trigger btn-floating btn-medium waves-effect waves-light red">赏</a>

    <!-- Modal Structure -->
    <div id="rewardModal" class="modal">
        <div class="modal-content">
            <a class="close modal-close"><i class="fas fa-times"></i></a>
            <h4 class="reward-title">你的赏识是我前进的动力</h4>
            <div class="reward-content">
                <div class="reward-tabs">
                    <ul class="tabs row">
                        <li class="tab col s6 alipay-tab waves-effect waves-light"><a href="#alipay">支付宝</a></li>
                        <li class="tab col s6 wechat-tab waves-effect waves-light"><a href="#wechat">微 信</a></li>
                    </ul>
                    <div id="alipay">
                        <img src="/medias/reward/alipay.png" class="reward-img" alt="支付宝打赏二维码">
                    </div>
                    <div id="wechat">
                        <img src="/medias/reward/we-chat.png" class="reward-img" alt="微信打赏二维码">
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
        $('.tabs').tabs();
    });
</script>

            
        </div>
    </div>

    

    

    

    
    <div class="livere-card card" data-aos="fade-up">
    <!-- 来必力City版安装代码 -->
    <div id="lv-container" class="card-content" data-id="city" data-uid="MTAyMC80Njg0OS8yMzM1MA">
        <script type="text/javascript">
            (function (d, s) {
                let j, e = d.getElementsByTagName(s)[0];
                if (typeof LivereTower === 'function') {
                    return;
                }

                j = d.createElement(s);
                j.src = 'https://cdn-city.livere.com/js/embed.dist.js';
                j.async = true;

                e.parentNode.insertBefore(j, e);
            })(document, 'script');
        </script>
        <noscript>为正常使用来必力评论功能请激活JavaScript。</noscript>
    </div>
    <!-- City版安装代码已完成 -->
</div>
    

    

    

    

    

    

<article id="prenext-posts" class="prev-next articles">
    <div class="row article-row">
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge left-badge text-color">
                <i class="fas fa-chevron-left"></i>&nbsp;上一篇</div>
            <div class="card">
                <a href="/posts/3093870655/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/23.jpg" class="responsive-img" alt="STM32CubeMX | 36-使用CAN总线进行双板通信（TJA1050）">
                        
                        <span class="card-title">STM32CubeMX | 36-使用CAN总线进行双板通信（TJA1050）</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本篇详细的记录了如何使用STM32CubeMX配置 STM32F407ZGT6 的硬件CAN接口与另一个开发板之间通信。
                        
                    </div>
                    <div class="publish-info">
                        <span class="publish-date">
                            <i class="far fa-clock fa-fw icon-date"></i>2021-06-01
                        </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/STM32CubeMX/" class="post-category">
                                    STM32CubeMX
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/STM32CubeMX-TJA1050/">
                        <span class="chip bg-color">STM32CubeMX TJA1050</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
        
        <div class="article col s12 m6" data-aos="fade-up">
            <div class="article-badge right-badge text-color">
                下一篇&nbsp;<i class="fas fa-chevron-right"></i>
            </div>
            <div class="card">
                <a href="/posts/3148061122/">
                    <div class="card-image">
                        
                        
                        <img src="/medias/featureimages/1.jpg" class="responsive-img" alt="STM32CubeMX | 34-使用硬件FSMC读写SRAM（IS62WV51216）">
                        
                        <span class="card-title">STM32CubeMX | 34-使用硬件FSMC读写SRAM（IS62WV51216）</span>
                    </div>
                </a>
                <div class="card-content article-content">
                    <div class="summary block-with-text">
                        
                            本篇详细的记录了如何使用STM32CubeMX配置 STM32F407ZGT6 的硬件FSMC外设与 SRAM 通信（IS62WV51216）。
                        
                    </div>
                    <div class="publish-info">
                            <span class="publish-date">
                                <i class="far fa-clock fa-fw icon-date"></i>2020-09-23
                            </span>
                        <span class="publish-author">
                            
                            <i class="fas fa-bookmark fa-fw icon-category"></i>
                            
                            <a href="/categories/STM32CubeMX/" class="post-category">
                                    STM32CubeMX
                                </a>
                            
                            
                        </span>
                    </div>
                </div>
                
                <div class="card-action article-tags">
                    
                    <a href="/tags/STM32CubeMX-SRAM/">
                        <span class="chip bg-color">STM32CubeMX SRAM</span>
                    </a>
                    
                </div>
                
            </div>
        </div>
        
    </div>
</article>

</div>



<!-- 代码块功能依赖 -->
<script type="text/javascript" src="/libs/codeBlock/codeBlockFuction.js"></script>

<!-- 代码语言 -->

<script type="text/javascript" src="/libs/codeBlock/codeLang.js"></script>


<!-- 代码块复制 -->

<script type="text/javascript" src="/libs/codeBlock/codeCopy.js"></script>


<!-- 代码块收缩 -->

<script type="text/javascript" src="/libs/codeBlock/codeShrink.js"></script>


    </div>
    <div id="toc-aside" class="expanded col l3 hide-on-med-and-down">
        <div class="toc-widget card" style="background-color: white;">
            <div class="toc-title"><i class="far fa-list-alt"></i>&nbsp;&nbsp;目录</div>
            <div id="toc-content"></div>
        </div>
    </div>
</div>

<!-- TOC 悬浮按钮. -->

<div id="floating-toc-btn" class="hide-on-med-and-down">
    <a class="btn-floating btn-large bg-color">
        <i class="fas fa-list-ul"></i>
    </a>
</div>


<script src="/libs/tocbot/tocbot.min.js"></script>
<script>
    $(function () {
        tocbot.init({
            tocSelector: '#toc-content',
            contentSelector: '#articleContent',
            headingsOffset: -($(window).height() * 0.4 - 45),
            collapseDepth: Number('0'),
            headingSelector: 'h1, h2, h3, h4, h5, h6'
        });

        // modify the toc link href to support Chinese.
        let i = 0;
        let tocHeading = 'toc-heading-';
        $('#toc-content a').each(function () {
            $(this).attr('href', '#' + tocHeading + (++i));
        });

        // modify the heading title id to support Chinese.
        i = 0;
        $('#articleContent').children('h1, h2, h3, h4, h5, h6').each(function () {
            $(this).attr('id', tocHeading + (++i));
        });

        // Set scroll toc fixed.
        let tocHeight = parseInt($(window).height() * 0.4 - 64);
        let $tocWidget = $('.toc-widget');
        $(window).scroll(function () {
            let scroll = $(window).scrollTop();
            /* add post toc fixed. */
            if (scroll > tocHeight) {
                $tocWidget.addClass('toc-fixed');
            } else {
                $tocWidget.removeClass('toc-fixed');
            }
        });

        
        /* 修复文章卡片 div 的宽度. */
        let fixPostCardWidth = function (srcId, targetId) {
            let srcDiv = $('#' + srcId);
            if (srcDiv.length === 0) {
                return;
            }

            let w = srcDiv.width();
            if (w >= 450) {
                w = w + 21;
            } else if (w >= 350 && w < 450) {
                w = w + 18;
            } else if (w >= 300 && w < 350) {
                w = w + 16;
            } else {
                w = w + 14;
            }
            $('#' + targetId).width(w);
        };

        // 切换TOC目录展开收缩的相关操作.
        const expandedClass = 'expanded';
        let $tocAside = $('#toc-aside');
        let $mainContent = $('#main-content');
        $('#floating-toc-btn .btn-floating').click(function () {
            if ($tocAside.hasClass(expandedClass)) {
                $tocAside.removeClass(expandedClass).hide();
                $mainContent.removeClass('l9');
            } else {
                $tocAside.addClass(expandedClass).show();
                $mainContent.addClass('l9');
            }
            fixPostCardWidth('artDetail', 'prenext-posts');
        });
        
    });
</script>

    

</main>




    <footer class="page-footer bg-color">
    
    <div class="container row center-align" style="margin-bottom: 15px !important;">
        <div class="col s12 m8 l8 copy-right">
            Copyright&nbsp;&copy;
            
                <span id="year">2016-2021</span>
            
            <span id="year">2016</span>
            <a href="/about" target="_blank">Mculover666</a>
            |&nbsp;Powered by&nbsp;<a href="https://hexo.io/" target="_blank">Hexo</a>
            |&nbsp;Theme&nbsp;<a href="https://github.com/blinkfox/hexo-theme-matery" target="_blank">Matery</a>
            <br>
            
            &nbsp;<i class="fas fa-chart-area"></i>&nbsp;站点总字数:&nbsp;<span
                class="white-color">118.4k</span>&nbsp;字
            
            
            
            
            
            
            <span id="busuanzi_container_site_pv">
                |&nbsp;<i class="far fa-eye"></i>&nbsp;总访问量:&nbsp;<span id="busuanzi_value_site_pv"
                    class="white-color"></span>&nbsp;次
            </span>
            
            
            <span id="busuanzi_container_site_uv">
                |&nbsp;<i class="fas fa-users"></i>&nbsp;总访问人数:&nbsp;<span id="busuanzi_value_site_uv"
                    class="white-color"></span>&nbsp;人
            </span>
            
            <br>
            
            <span id="sitetime">载入运行时间...</span>
            <script>
                function siteTime() {
                    var seconds = 1000;
                    var minutes = seconds * 60;
                    var hours = minutes * 60;
                    var days = hours * 24;
                    var years = days * 365;
                    var today = new Date();
                    var startYear = "2016";
                    var startMonth = "8";
                    var startDate = "28";
                    var startHour = "0";
                    var startMinute = "0";
                    var startSecond = "0";
                    var todayYear = today.getFullYear();
                    var todayMonth = today.getMonth() + 1;
                    var todayDate = today.getDate();
                    var todayHour = today.getHours();
                    var todayMinute = today.getMinutes();
                    var todaySecond = today.getSeconds();
                    var t1 = Date.UTC(startYear, startMonth, startDate, startHour, startMinute, startSecond);
                    var t2 = Date.UTC(todayYear, todayMonth, todayDate, todayHour, todayMinute, todaySecond);
                    var diff = t2 - t1;
                    var diffYears = Math.floor(diff / years);
                    var diffDays = Math.floor((diff / days) - diffYears * 365);
                    var diffHours = Math.floor((diff - (diffYears * 365 + diffDays) * days) / hours);
                    var diffMinutes = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours) /
                        minutes);
                    var diffSeconds = Math.floor((diff - (diffYears * 365 + diffDays) * days - diffHours * hours -
                        diffMinutes * minutes) / seconds);
                    if (startYear == todayYear) {
                        document.getElementById("year").innerHTML = todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffDays + " 天 " + diffHours +
                            " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    } else {
                        document.getElementById("year").innerHTML = startYear + " - " + todayYear;
                        document.getElementById("sitetime").innerHTML = "本站已安全运行 " + diffYears + " 年 " + diffDays +
                            " 天 " + diffHours + " 小时 " + diffMinutes + " 分钟 " + diffSeconds + " 秒";
                    }
                }
                setInterval(siteTime, 1000);
            </script>
            
            <br>
            
            <span id="icp"><img src="/medias/icp.png" style="vertical-align: text-bottom;" />
                <a href="https://beian.miit.gov.cn" target="_blank">陇ICP备19002222号</a>
            </span>
            
        </div>
        <div class="col s12 m4 l4 social-link social-statis">
    <a href="https://github.com/Mculover666" class="tooltipped" target="_blank" data-tooltip="访问我的GitHub" data-position="top" data-delay="50">
        <i class="fab fa-github"></i>
    </a>



    <a href="mailto:mculover666@foxmail.com" class="tooltipped" target="_blank" data-tooltip="邮件联系我" data-position="top" data-delay="50">
        <i class="fas fa-envelope-open"></i>
    </a>







    <a href="tencent://AddContact/?fromId=50&fromSubId=1&subcmd=all&uin=2412828003" class="tooltipped" target="_blank" data-tooltip="QQ联系我: 2412828003" data-position="top" data-delay="50">
        <i class="fab fa-qq"></i>
    </a>





    <a href="https://www.zhihu.com/people/wang-shi-wei-67-38/activities" class="tooltipped" target="_blank" data-tooltip="关注我的知乎: https://www.zhihu.com/people/wang-shi-wei-67-38/activities" data-position="top" data-delay="50">
        <i class="fab fa-zhihu1">知</i>
    </a>



    <a href="/atom.xml" class="tooltipped" target="_blank" data-tooltip="RSS 订阅" data-position="top" data-delay="50">
        <i class="fas fa-rss"></i>
    </a>

</div>
    </div>
</footer>

<div class="progress-bar"></div>


    <!-- 搜索遮罩框 -->
<div id="searchModal" class="modal">
    <div class="modal-content">
        <div class="search-header">
            <span class="title"><i class="fas fa-search"></i>&nbsp;&nbsp;搜索</span>
            <input type="search" id="searchInput" name="s" placeholder="请输入搜索的关键字"
                   class="search-input">
        </div>
        <div id="searchResult"></div>
    </div>
</div>

<script type="text/javascript">
$(function () {
    var searchFunc = function (path, search_id, content_id) {
        'use strict';
        $.ajax({
            url: path,
            dataType: "xml",
            success: function (xmlResponse) {
                // get the contents from search data
                var datas = $("entry", xmlResponse).map(function () {
                    return {
                        title: $("title", this).text(),
                        content: $("content", this).text(),
                        url: $("url", this).text()
                    };
                }).get();
                var $input = document.getElementById(search_id);
                var $resultContent = document.getElementById(content_id);
                $input.addEventListener('input', function () {
                    var str = '<ul class=\"search-result-list\">';
                    var keywords = this.value.trim().toLowerCase().split(/[\s\-]+/);
                    $resultContent.innerHTML = "";
                    if (this.value.trim().length <= 0) {
                        return;
                    }
                    // perform local searching
                    datas.forEach(function (data) {
                        var isMatch = true;
                        var data_title = data.title.trim().toLowerCase();
                        var data_content = data.content.trim().replace(/<[^>]+>/g, "").toLowerCase();
                        var data_url = data.url;
                        data_url = data_url.indexOf('/') === 0 ? data.url : '/' + data_url;
                        var index_title = -1;
                        var index_content = -1;
                        var first_occur = -1;
                        // only match artiles with not empty titles and contents
                        if (data_title !== '' && data_content !== '') {
                            keywords.forEach(function (keyword, i) {
                                index_title = data_title.indexOf(keyword);
                                index_content = data_content.indexOf(keyword);
                                if (index_title < 0 && index_content < 0) {
                                    isMatch = false;
                                } else {
                                    if (index_content < 0) {
                                        index_content = 0;
                                    }
                                    if (i === 0) {
                                        first_occur = index_content;
                                    }
                                }
                            });
                        }
                        // show search results
                        if (isMatch) {
                            str += "<li><a href='" + data_url + "' class='search-result-title'>" + data_title + "</a>";
                            var content = data.content.trim().replace(/<[^>]+>/g, "");
                            if (first_occur >= 0) {
                                // cut out 100 characters
                                var start = first_occur - 20;
                                var end = first_occur + 80;
                                if (start < 0) {
                                    start = 0;
                                }
                                if (start === 0) {
                                    end = 100;
                                }
                                if (end > content.length) {
                                    end = content.length;
                                }
                                var match_content = content.substr(start, end);
                                // highlight all keywords
                                keywords.forEach(function (keyword) {
                                    var regS = new RegExp(keyword, "gi");
                                    match_content = match_content.replace(regS, "<em class=\"search-keyword\">" + keyword + "</em>");
                                });

                                str += "<p class=\"search-result\">" + match_content + "...</p>"
                            }
                            str += "</li>";
                        }
                    });
                    str += "</ul>";
                    $resultContent.innerHTML = str;
                });
            }
        });
    };

    searchFunc('/search.xml', 'searchInput', 'searchResult');
});
</script>

    <!-- 回到顶部按钮 -->
<div id="backTop" class="top-scroll">
    <a class="btn-floating btn-large waves-effect waves-light" href="#!">
        <i class="fas fa-arrow-up"></i>
    </a>
</div>


    <script src="/libs/materialize/materialize.min.js"></script>
    <script src="/libs/masonry/masonry.pkgd.min.js"></script>
    <script src="/libs/aos/aos.js"></script>
    <script src="/libs/scrollprogress/scrollProgress.min.js"></script>
    <script src="/libs/lightGallery/js/lightgallery-all.min.js"></script>
    <script src="/js/matery.js"></script>

    <!-- Baidu Analytics -->

<script>
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?6e1efa0988723a1db6356535db2ae6bd";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <script src="/libs/others/clicklove.js" async="async"></script>
    
    
    <script async src="/libs/others/busuanzi.pure.mini.js"></script>
    

    

    

    <!--腾讯兔小巢-->
    
    

    

    

    
    <script src="/libs/instantpage/instantpage.js" type="module"></script>
    

</body>

</html>
